<template>
  <!--  公话-综合查询-学生充值查询 -->
  <div class="student_recharge_inquiry">
    <el-form :inline="true" :model="elform" class="demo-form-inline" size="mini">
      <el-form-item label="卡号:">
        <el-input v-model="cardNumber" placeholder="卡号" size="mini" clearable style="width:130px"></el-input>
      </el-form-item>
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="gosearch" size="mini" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportexeclto" size="mini">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="agentlist"
      key="table1"
      style="width:100%"
      border="border"
      class="tablebox"
      highlight-current-row
      :header-cell-style="headFirst"
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column prop="cardNumber" label="充值卡号"></el-table-column>
      <el-table-column label="充值金额(元)">
        <template slot-scope="scope">
          <div>{{scope.row.adjustAmount | amountjiao}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="充值人"></el-table-column>
      <el-table-column prop="departmentName" label="归属班级"></el-table-column>
      <el-table-column prop="adjustTime" min-width="150px" label="充值日期"></el-table-column>
      <el-table-column label="充值前余额(元)">
        <template slot-scope="scope">
          <div>{{scope.row.adjusted | amountjiao}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="adjustedBalance" label="充值后余额(元)">
        <template slot-scope="scope">
          <div>{{scope.row.adjustedBalance | amountjiao}}</div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChangepage"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      elform: {},
      cardNumber: '',
      // 时间
      datetime: [],
      time: [],
      pageNum: 1,
      pageSize: 20,
      recordCount: 0,
      currentPage: 1,
      searchInfo: {},
      agentlist: [],
      agentlistout: [],
      dataListLoading: false,
      filename: '学生充值记录'
    }
  },
  created() {
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  methods: {
    gosearch() {
      this.pageNum = 1
      this.searchInfo = {}
      if (this.cardNumber !== null && this.cardNumber !== '') {
        this.searchInfo.cardNumber = this.cardNumber
      }
      this.searchInfo.startTime = this.datetime[0] + ' ' + this.time[0]
      this.searchInfo.endTime = this.datetime[1] + ' ' + this.time[1]
      this.searchInfo.unitGuid = this.changeschoolid
      this.studentinit()
    },
    exportexeclto() {
      if (this.agentlist.length == 0) {
        this.$message({
          message: '查询数据后导出',
          type: 'warning'
        })
        return false
      }
      this.outexecel()
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    outexecel() {
      this.$http({
        url: this.$http.adornUrl('/voip/recharge/user-recharge-search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: 1,
            pageSize: this.recordCount,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          this.agentlistout = data.messageContent.data
          for (var i = 0; i < this.agentlistout.length; i++) {
            this.agentlistout[i].adjustAmount = this.amountjia(
              this.agentlistout[i].adjustAmount
            )
            this.agentlistout[i].adjusted = this.amountjia(
              this.agentlistout[i].adjusted
            )
            this.agentlistout[i].adjustedBalance = this.amountjia(
              this.agentlistout[i].adjustedBalance
            )
          }
          setTimeout(() => {
            this.export2Excel()
          }, 0.1)
        }
      })
    },
    export2Excel() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const multiHeader = []
        const multiHeader2 = []
        const merges = []
        const { export_json_to_excel } = require('../../../excel/Export2Excel')
        const tHeader = [
          '充值卡号',
          '充值金额(元)',
          '充值人',
          '归属班级',
          '充值日期',
          '充值前余额(元)',
          '充值后余额(元)'
        ]
        const filterVal = [
          'cardNumber',
          'adjustAmount',
          'userName',
          'departmentName',
          'adjustTime',
          'adjusted',
          'adjustedBalance'
        ]
        const list = this.agentlistout
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: this.filename + nowtime
        })
      })
    },
    handleSizeChangepage(val) {
      this.pageSize = val
      this.studentinit()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.studentinit()
    },
    //学生充值记录列表
    studentinit() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/voip/recharge/user-recharge-search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          this.agentlist = data.messageContent.data || []
          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
          this.dataListLoading = false
          if (this.agentlist.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.student_recharge_inquiry {
  padding: 10px;
  .demo-form-inline,
  .demo-form-inline1 {
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 250px;
    }
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>